package unsafe;

import java.util.concurrent.CountDownLatch;
//计数器
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        // 总数是6
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "==> Go Out");
                countDownLatch.countDown(); // 每个线程都数量 -1；要等六个线程都执行完毕才可以关
            },String.valueOf(i)).start();
        }
        countDownLatch.await(); // 等待计数器归零 然后向下执行
        System.out.println("close door");
    }
}
/*
没有这句的时候countDownLatch.await(); // 等待计数器归零 然后向下执行
没有的时候计数器没有归零也可以往下走
执行结果：
close door
2==> Go Out
1==> Go Out
3==> Go Out
4==> Go Out
5==> Go Out
6==> Go Out
有了那句的时候执行结果:
1==> Go Out
2==> Go Out
3==> Go Out
4==> Go Out
5==> Go Out
6==> Go Out
close door
 */